#include "stdafx.h"
DTBFile::DTBFile()
{
	this->file= new TFile;
}
DTBFile::~DTBFile()
{
	delete file;
}
FILE * DTBFile::GetFile()
{
	return file->Get();
}
void DTBFile::Open(char * filename)
{
	file->Open(filename,P_ADD|P_UPDATE|P_BINARY);
}
uint DTBFile::GetPing()
{
	uint ping;
	file->Seek(sizeof(time_t),S_CURRENT);
	file->Read(&ping,sizeof(ping),1);
	file->Seek(-sizeof(time_t)-sizeof(uint),S_CURRENT);
	return ping;
}
uint DTBFile::GetPosition()
{
	return ftell(file->Get());
}
time_t DTBFile::GetTime()
{
	time_t tt;
	file->Read(&tt,sizeof(time_t),1);
	file->Seek(-sizeof(time_t),S_CURRENT);
	return tt;
}
uint DTBFile::GoToBegin()
{
	file->Seek(0,S_BEGIN);
	return ftell(file->Get());
}
uint DTBFile::GoToBlock(int nBlockNum)
{
	if (nBlockNum<0)
		nBlockNum+=-2*nBlockNum;
	uint seekto=nBlockNum*12;
	file->Seek(seekto,S_BEGIN);
	return ftell(file->Get());
}
uint DTBFile::GoToEnd()
{
	file->Seek(0,S_END);
	return ftell(file->Get());	
}
uint DTBFile::GoToNextBlock()
{
	file->Seek(sizeof(time_t)+sizeof(uint),S_CURRENT);
	return ftell(file->Get());
}
uint DTBFile::GoToPrevBlock()
{
	file->Seek(-sizeof(time_t)-sizeof(uint),S_CURRENT);
	return ftell(file->Get());
}
uint DTBFile::Seek(int uiCount)
{
	file->Seek(uiCount,S_CURRENT);
	return ftell(file->Get());
}
uint DTBFile::SeekToTime(time_t ttTime)
{
	uint curpos=GetPosition(), findposition=0;
	for (SetPosition(0); GetTime()<ttTime; GoToNextBlock());
	findposition=GetPosition();
	return findposition;
}
void DTBFile::SetPosition(uint uiPosition)
{
	fseek(file->Get(),uiPosition,S_BEGIN);
}
void DTBFile::Write(time_t ttTime, uint uiPing)
{
	file->Write(&ttTime,sizeof(time_t),1);
	file->Write(&uiPing,sizeof(uint),1);
}
